package com.RedRiver.hospitalsystem.personal_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.personal_service.common.Personal_serviceResultCode;
import com.RedRiver.hospitalsystem.personal_service.common.exception.DepartmentException;
import com.RedRiver.hospitalsystem.personal_service.common.exception.SubjectException;
import com.RedRiver.hospitalsystem.personal_service.infra.mapper.DepartmentMapper;
import com.RedRiver.hospitalsystem.personal_service.infra.mapper.SubjectMapper;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Department;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Doctor;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Subject;
import com.RedRiver.hospitalsystem.personal_service.service.DepartmentService;
import com.RedRiver.hospitalsystem.personal_service.service.Form.*;
import com.RedRiver.hospitalsystem.personal_service.service.SubjectService;
import com.RedRiver.hospitalsystem.personal_service.service.info.SubjectPageInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 分页查询所有科室
     * @param form
     * @return
     */
    @Override
    public IPage<SubjectPageInfo> findPageSubject(PageForm form) {
        Page<Subject> subjectInfoPage = new Page<>();
        subjectInfoPage = this.baseMapper.selectPage(new Page<>(form.getPage() == 0 ? 1 : form.getPage(), form.getPageSize() == 0 ? 2 : form.getPageSize()),null);
        List<Subject> subjectInfoList = subjectInfoPage.getRecords();
        List<SubjectPageInfo> subjectPageInfoList = BeanUtil.copyToList(subjectInfoList, SubjectPageInfo.class);
        Page<SubjectPageInfo> page = new Page<>();
        page.setRecords(subjectPageInfoList);
        page.setCurrent(subjectInfoPage.getCurrent());
        page.setSize(subjectInfoPage.getSize());
        page.setTotal(subjectInfoPage.getTotal());
        return page;
    }

    /**
     * 分页查看科室名前面相同的所有科室
     * @param subjectNamePageForm
     * @return
     */
    @Override
    public IPage<SubjectPageInfo> findPageSubjectName(SubjectNamePageForm subjectNamePageForm) {
        Page<Subject> subjectInfoPage = new Page<>();
        subjectInfoPage = this.baseMapper.selectPage(new Page<>(subjectNamePageForm.getPage() == 0 ? 1 : subjectNamePageForm.getPage(),
                subjectNamePageForm.getPageSize() == 0 ? 2 : subjectNamePageForm.getPageSize()),
                new QueryWrapper<Subject>().likeRight("subject_name",subjectNamePageForm.getSubjectName()));
        List<Subject> subjectInfoList = subjectInfoPage.getRecords();
        List<SubjectPageInfo> subjectPageInfoList = BeanUtil.copyToList(subjectInfoList, SubjectPageInfo.class);
        Page<SubjectPageInfo> page = new Page<>();
        page.setRecords(subjectPageInfoList);
        page.setCurrent(subjectInfoPage.getCurrent());
        page.setSize(subjectInfoPage.getSize());
        page.setTotal(subjectInfoPage.getTotal());
        return page;
    }

    /**
     * 根据部门id分页查看所有该部门下的科室
     * @param subjectDepartmentPageForm
     * @return
     */
    @Override
    public IPage<SubjectPageInfo> findAllSubjectDepartment(SubjectDepartmentPageForm subjectDepartmentPageForm) {
        Page<Subject> subjectInfoPage = new Page<>();
        subjectInfoPage = this.baseMapper.selectPage(new Page<>(subjectDepartmentPageForm.getPage() == 0 ? 1 : subjectDepartmentPageForm.getPage(),
                          subjectDepartmentPageForm.getPageSize() == 0 ? 2 : subjectDepartmentPageForm.getPageSize()),
                          new QueryWrapper<Subject>().eq("department_id",subjectDepartmentPageForm.getDepartmentId()));
        List<Subject> subjectInfoList = subjectInfoPage.getRecords();
        List<SubjectPageInfo> subjectPageInfoList = BeanUtil.copyToList(subjectInfoList, SubjectPageInfo.class);
        Page<SubjectPageInfo> page = new Page<>();
        page.setRecords(subjectPageInfoList);
        page.setCurrent(subjectInfoPage.getCurrent());
        page.setSize(subjectInfoPage.getSize());
        page.setTotal(subjectInfoPage.getTotal());
        return page;
    }

    /**
     * 新增科室
     * @param subjectForm
     * @return
     */
    @Override
    public Result addSubject(SubjectForm subjectForm) {
        Department department_id = departmentMapper.selectOne(new QueryWrapper<Department>().eq("department_id", subjectForm.getDepartmentId()));
        if (department_id==null) throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_NOT_ERROR);
        Subject subject_name = subjectMapper.selectOne(new QueryWrapper<Subject>().eq("subject_name", subjectForm.getSubjectName()));
        if (subject_name!=null) throw new SubjectException(Personal_serviceResultCode.SUBJECT_NAME_EXISTED);
        Subject subject_address = subjectMapper.selectOne(new QueryWrapper<Subject>().eq("subject_address", subjectForm.getSubjectAddress()));
        if (subject_address!=null) throw new SubjectException(Personal_serviceResultCode.SUBJECT_ADDRESS_EXISTED);
        int insert = subjectMapper.insert(BeanUtil.copyProperties(subjectForm, Subject.class));
        if (insert==0) throw new SubjectException(Personal_serviceResultCode.SUBJECT_ADD_ERROR);
        return Result.getInstance().setMsg("新增成功");
    }

    /**
     * 修改科室信息
     * @param newSubjectForm
     * @return
     */
    @Override
    public Result changeSubject(NewSubjectForm newSubjectForm) {
        Subject subject_id = subjectMapper.selectOne(new QueryWrapper<Subject>().eq("subject_id", newSubjectForm.getSubjectId()));
        if (subject_id==null) throw new SubjectException(Personal_serviceResultCode.SUBJECT_NOT_EXISTED);
        Department department_id = departmentMapper.selectOne(new QueryWrapper<Department>().eq("department_id", newSubjectForm.getDepartmentId()));
        if (department_id==null) throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_NOT_ERROR);
        List<Subject> subject_name = subjectMapper.selectList(new QueryWrapper<Subject>().eq("subject_name", newSubjectForm.getSubjectName()));
        for (Subject subject : subject_name) {
            if (subject.getSubjectId()!=newSubjectForm.getSubjectId()) throw new SubjectException(Personal_serviceResultCode.SUBJECT_NAME_EXISTED);
        }
        List<Subject> subject_address = subjectMapper.selectList(new QueryWrapper<Subject>().eq("subject_address", newSubjectForm.getSubjectAddress()));
        for (Subject subjectAddress : subject_address) {
            if (subjectAddress.getSubjectId()!=newSubjectForm.getSubjectId())throw new SubjectException(Personal_serviceResultCode.SUBJECT_ADDRESS_EXISTED);
        }
        Subject entity = BeanUtil.copyProperties(newSubjectForm, Subject.class);
        int update = subjectMapper.update(entity,new QueryWrapper<Subject>().eq("subject_id",entity.getSubjectId()));
        if (update==0)throw new SubjectException(Personal_serviceResultCode.SUBJECT_UPDATE_ERROR);
        return Result.getInstance().setMsg("科室信息修改成功");
    }

    /**
     * 删除科室
     * @param subjectId
     * @return
     */
    @Override
    public Result delSubject(Integer subjectId) {
        Subject subject_id = subjectMapper.selectOne(new QueryWrapper<Subject>().eq("subject_id", subjectId));
        if (subject_id==null) throw new SubjectException(Personal_serviceResultCode.SUBJECT_NOT_EXISTED);
        return Result.getInstance().setMsg("删除科室成功");
    }



}
